////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
//  Deck
//
//  Created by psvn on 4/18/12.
//  Copyright (c) 2012 Playsoft. All rights reserved.
//
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

#import "Deck.h"
#import "GDataDefines.h"

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

@interface Deck ()

@property (nonatomic, retain) UIImage *backImage;
@property (nonatomic, copy) NSArray *cards;
@property (nonatomic, copy) NSArray *suits;

@end

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

@implementation Deck

@synthesize backImage = _backImage;
@synthesize cards = _cards;
@synthesize suits = _suits;


- (id)init
{
    return [self initWithDeckFilename:nil];
}

- (id)initWithDeckFilename:(NSString *)aDeckFilename
{
    self = [super init];
    if (nil != self)
    {
        NSData *deckXMLData = [NSData dataWithContentsOfFile:aDeckFilename];
        NSAssert(nil != deckXMLData, @"");
        GDataXMLDocument *deckDocument = [[[GDataXMLDocument alloc] initWithData:deckXMLData options:0
        	error:NULL] autorelease];
 
         NSAssert(nil != deckDocument, @"");
        GDataXMLElement *rootElement = [deckDocument rootElement];
        NSAssert([[rootElement name] isEqualToString:@"Deck"], @"");
        
        NSString *backImageName = [[rootElement attributeForName:@"backImage"] stringValue];
        
        UIImage *backImage = [UIImage imageNamed:backImageName];
        NSAssert(nil != backImage, @"");
        self.backImage = backImage;
        
        NSArray *suitElements = [rootElement elementsForName:@"Suit"];

        NSAssert(4 == [suitElements count], @"");
        NSMutableArray *cards = [NSMutableArray arrayWithCapacity:52];
        NSMutableArray *suits = [NSMutableArray arrayWithCapacity:4];
         
        for (GDataXMLElement *suitElement in suitElements)
        {
            NSString *suitName = [[suitElement attributeForName:@"name"] stringValue];
            NSAssert(nil != suitName, @"");
            
            CardSuit suit = kCardSuitSpades;
            if ([suitName isEqualToString:@"spades"])
            {
                suit = kCardSuitSpades;
            }
            else if ([suitName isEqualToString:@"clubs"])
            {
                suit = kCardSuitClubs;
            }
            else if ([suitName isEqualToString:@"diamonds"])
            {
                suit = kCardSuitDiamonds;
            }
            else if ([suitName isEqualToString:@"hearts"])
            {
                suit = kCardSuitHearts;
            }
            else
            {
                NSAssert(NO, @"Invalid suite name");
            }
            
            NSMutableArray *suiteCards = [NSMutableArray array];
            NSArray *cardElements = [suitElement elementsForName:@"Card"];
            NSAssert(13 == [cardElements count], @"");
            for (NSUInteger cardIndex = 0; cardIndex < 13; ++cardIndex)
            {
                GDataXMLElement *cardElement = [cardElements objectAtIndex:cardIndex];
                NSString *cardImageName = [cardElement stringValue];
                
                Card *card = [[[Card alloc] initWithRank:(CardRank)(cardIndex)
                	suit:suit imageName:cardImageName] autorelease];
                [cards addObject:card];
                [suiteCards addObject:card];
            }
            
            [suits addObject:suiteCards];
        }

        self.cards = cards;
        self.suits = suits;
    }
    
    return self;
}

- (void)dealloc
{
    self.backImage = nil;
    self.cards = nil;
    self.suits = nil;
    [super dealloc];
}
- (Card *)cardWithRank:(int) numCard
{
    NSLog(@"numCard = %d", numCard);
    if(numCard < 0 || numCard >=52)
    {
        return NULL;
    }
    int aSuit = numCard / 13;
    int aCardRank = numCard % 13;
     NSLog(@"aSuit = %d", aSuit);
     NSLog(@"aCardRank = %d", aCardRank);
    NSArray *suiteCards = [self.suits objectAtIndex:(NSUInteger)(aSuit)];
    Card *result = [suiteCards objectAtIndex:(NSUInteger)(aCardRank)];
    return result;
}
- (Card *)cardWithRank:(CardRank)aCardRank suit:(CardSuit)aSuit
{
    NSArray *suiteCards = [self.suits objectAtIndex:(NSUInteger)(aSuit)];
    Card *result = [suiteCards objectAtIndex:(NSUInteger)(aCardRank)];
    return result;
}

- (Card *)randomCard
{
    return [self.cards objectAtIndex:(random() % [self.cards count])];
}

@end

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
